	<?php
/*
 * Post the recipe
 */
// authenticate user first
F3::call('authentication.php');
if (F3::exists('auth_error'))
{
  // authentication failed
  return;
}

if(!F3::exists('POST.name')|| !F3::exists('POST.yield') ||!F3::exists('POST.prep_time')||!F3::exists('POST.inactive_time')||!F3::exists('POST.cook_time')){
	header('HTTP/1.1 500 Internal Server Error');
	header("Content-Type: application/json");
	$err = array("error_message" => "Need to input more information");
	echo json_encode($err);
	return;
}

$param = array('1'=> F3::get('POST.name'),'2'=>F3::get('SESSION.user_id'));

$result = DB::sql("select * from recipes where name = ? AND author = ?", $param);

if(count($result)>0){
	header('HTTP/1.1 500 Internal Server Error');
	header("Content-Type: application/json");
	$err = array("error_message" => "the recipe already exist");
	echo json_encode($err);
	return;
}

$param = array('1'=>F3::get('POST.name'), '2'=>F3::get('SESSION.user_id'), '3'=>F3::get('POST.prep_time'), '4'=>F3::get('POST.inactive_time'),'5'=>F3::get('POST.cook_time'), '6'=>F3::get('POST.yield'));
DB::sql("insert into recipes(name, author, prep_time, inactive_time, cook_time, yield) values (?, ?, ?, ?, ?, ?)",$param);

$rid = 0;
$db = F3::get('DB');
$pdo = $db->pdo;
$rid = $pdo->lastInsertId();

/*$param = array('1'=>$rid,'2'=>F3::get('SESSION.user_id'), '3'=>F3::get('POST.rating'));
DB::sql("INSERT into ratings(rid,uid,rating) VALUES (?,?,?)",$param);*/

$fields = array('link','photo','privacy');

foreach($fields as $field){
	if(F3::exists("POST.$field")){
		$param = array('1' => F3::get("POST.$field"), '2' => $rid);
		DB::sql("update recipes set $field = ? where rid = ?", $param);
	}
}
if(F3::exists('POST.ingredient')){
	$ingredients = ucwords(strtolower(F3::get('POST.ingredient')));
	$ingredient = explode(":", $ingredients);
	foreach($ingredient as $a){
		$type = explode("*", $a);
		$param = array('1'=>ucwords(strtolower($type[2])));
		$m = DB::sql("SELECT id from measurements where name = ?",$param);
		if(count($m)==0){
			DB::sql("INSERT INTO measurements (name) VALUES (?)",$param);
			$db = F3::get('DB');
			$pdo = $db->pdo;
			$mid = $pdo->lastInsertId();
		}
		else{
			$mid = $m[0]['id'];
		}
		$param = array('1'=>ucwords(strtolower($type[0])));
		$ingredientid = DB::sql("SELECT id from ingredients where name = ?", $param);
		if(count($ingredientid)==0){//there doesn't exist this ingredients yet, we need to add the id and ingredient
			DB::sql("INSERT INTO ingredients(name) VALUES (?)", $param);
			$db = F3::get('DB');
			$pdo = $db->pdo;
			$in_id = $pdo->lastInsertId();
			$param = array('1'=>$rid,'2'=>$in_id,'3'=>$type[1],'4'=>$mid, '5'=>$type[3]);
			DB::sql("INSERT INTO recipe_ingredients(rid, inid, quantity, mid, optional) VALUES (?,?,?,?,?)",$param);
		}
		else{
			$param = array('1'=>$rid,'2'=>$ingredientid[0]['id'],'3'=>$type[1],'4'=>$mid, '5'=>$type[3]);
			DB::sql("INSERT INTO recipe_ingredients(rid, inid, quantity, mid, optional) VALUES (?,?,?,?,?)",$param);
		}
	}
}

if(F3::exists('POST.tag')){
	$tags = ucwords(strtolower(F3::get('POST.tag')));
	$tag = explode(":", $tags);
	foreach($tag as $a){
		$param = array('1'=>ucwords(strtolower($a)));
		$tag_id = DB::sql("SELECT id from tags where name = ?", $param);
		if(count($tag_id)==0 && strcmp($a,"")!=0){//there doesn't exist this tag yet, we need to add the id and tag
			DB::sql("INSERT INTO tags(name) VALUES (?)", $param);
			$db = F3::get('DB');
			$pdo = $db->pdo;
			$tag_id = $pdo->lastInsertId();
			$param = array('1'=>$rid,'2'=>$tag_id);
			DB::sql("INSERT INTO recipe_tags(rid, tid) VALUES (?,?)",$param);
		}
		else{//there exists the tag
			$param = array('1'=>$rid, '2'=>$tag_id[0]['id']);
			DB::sql("INSERT INTO recipe_tags(rid, tid) VALUES (?,?)",$param);
		}
	}
}


// directions
if(F3::exists('POST.direction')):
	$dirs = explode("|",F3::get('POST.direction'));
	for($i=0; $i<count($dirs); $i++):
		$param = array('1'=>$rid, '2'=>($i+1), '3'=>$dirs[$i]);
		DB::sql("INSERT INTO directions(rid, step, details) VALUES (?,?,?)",$param);
	endfor;
endif;

header('HTTP/1.1 201');
header("Content-Type: application/json");
echo json_encode(array("success" => "1", "rid" => $rid));
return
?>